import { FastifyInstance } from "fastify";
import { Static, Type } from "@sinclair/typebox";
import { Event } from "@db";

const eventBody = Type.Object({
    title: Type.String(),
    skills: Type.Array(Type.String(), { maxItems: 10 }),
    date: Type.String(),
});

export const organization = async (fastify: FastifyInstance) => {
    fastify.post<{
        Body: Static<typeof eventBody>;
    }>(
        "/organization/event/create",
        {
            preHandler: fastify.auth("organization"),
            schema: {
                tags: ["organization"],
                body: eventBody,
            },
        },
        async (req, res) => {
            const { title, skills, date } = req.body;

            const event = await Event.create({
                title,
                skills,
                date,
                organizationId: req.organization.id,
            });

            return res.send(event);
        }
    );
};
